/**
 * Copyright (c) 2009, http://www.softwareinlife.com. All rights reserved.
 * 
 * Redistribution and use in source and binary forms, 
 * with or without modification, are permitted provided that 
 * the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, 
 *    this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright notice, 
 *    this list of conditions and the following disclaimer in the documentation 
 *    and/or other materials provided with the distribution.
 * 
 * 3. Neither the name of the http://www.softwareinlife.com nor the names of its 
 *    contributors may be used to endorse or promote products derived from 
 *    this software without specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE SOFTWAREINLIFE AND CONTRIBUTORS "AS IS" 
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
 * ARE DISCLAIMED. 
 * IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
package com.softwareinlife.vision.android.data.dao;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;

import com.softwareinlife.vision.android.data.exceptions.DatabaseConnectionException;
import com.softwareinlife.vision.android.data.exceptions.EventCreateException;
import com.softwareinlife.vision.android.data.exceptions.PlanCreateException;
import com.softwareinlife.vision.android.data.exceptions.ScheduleCreateException;
import com.softwareinlife.vision.android.data.exceptions.UserCreateException;
import com.softwareinlife.vision.android.data.exceptions.VisionCreateException;

/**
 * @author Jang, Sun-Jin (jangsunjin@softwareinlife.com)
 * @date 2009. 10. 10.
 */
public class BaseDAO {

	/**
	 * Database Context
	 */
	protected Context ctx = null;

	/**
	 * SQLite Database
	 */
	protected SQLiteDatabase db = null;

	/**
	 * Cursor Factory
	 */
	protected CursorFactory factory = null;

	/**
	 * Constructor
	 * 
	 * @throws DatabaseConnectionException
	 */
	public BaseDAO(Context ctx) throws DatabaseConnectionException {
		super();

		if (ctx != null) {
			this.ctx = ctx;

			// Initialize Database
			initDatabase();
		}
	}

	public BaseDAO(SQLiteDatabase db) {
		this.db = db;
	}

	/**
	 * Initialize Database
	 * 
	 * @throws DatabaseConnectionException
	 */
	private void initDatabase() throws DatabaseConnectionException {
		if (db == null) {
			db = ctx.openOrCreateDatabase(BaseConstants.DATABASE_NAME,
					SQLiteDatabase.OPEN_READWRITE
							+ SQLiteDatabase.CREATE_IF_NECESSARY, factory);

			if (!db.isOpen()) {
				throw new DatabaseConnectionException("SQLite is not opened!");
			}
		}
	}

	/**
	 * Create Database
	 * 
	 * @throws DatabaseConnectionException
	 */
	public void createDatabase() throws DatabaseConnectionException {
		if (db == null) {
			db = ctx.openOrCreateDatabase(BaseConstants.DATABASE_NAME,
					SQLiteDatabase.OPEN_READWRITE
							+ SQLiteDatabase.CREATE_IF_NECESSARY, factory);
		}
	}

	/**
	 * Create Tables
	 * 
	 * @throws DatabaseConnectionException
	 */
	public void createTables() throws DatabaseConnectionException {
		// User DAO
		try {
			UserDAO userDAO = new UserDAO(db);
			userDAO.createTable();
		} catch (UserCreateException e) {
			e.printStackTrace();
		}

		// Vision DAO
		try {
			VisionDAO visionDAO = new VisionDAO(db);
			visionDAO.createTable();
		} catch (VisionCreateException e) {
			e.printStackTrace();
		}
		
		// Plan DAO
		try {
			PlanDAO planDAO = new PlanDAO(db);
			planDAO.createTable();
		} catch (PlanCreateException e) {
			e.printStackTrace();
		}	
		
		// Schedule DAO
		try {
			ScheduleDAO scheduleDAO = new ScheduleDAO(db);
			scheduleDAO.createTable();
		} catch (ScheduleCreateException e) {
			e.printStackTrace();
		}	
		
		// Event DAO
		try {
			EventDAO visionDAO = new EventDAO(db);
			visionDAO.createTable();
		} catch (EventCreateException e) {
			e.printStackTrace();
		}	
	}

	/**
	 * Begin Transaction
	 */
	public void beginTransaction() {
		db.beginTransaction();
	}

	/**
	 * End Transaction
	 */
	public void endTransaction() {
		db.endTransaction();
	}

	/**
	 * @return the database
	 */
	public SQLiteDatabase getDatabase() {
		return db;
	}

	/**
	 * @return the cursor
	 */
	public CursorFactory getCursor() {
		return factory;
	}

	/**
	 * @param cursor
	 *            the cursor to set
	 */
	public void setCursor(CursorFactory cursor) {
		this.factory = cursor;
	}

	/**
	 * Close Database
	 */
	public void close() {
		if (db != null) {
			db.close();
		}
	}
}
